/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
All rights reserved.

This software was developed in the APRIL Robotics Lab under the
direction of Edwin Olson, ebolson@umich.edu. This software may be
available under alternative licensing terms; contact the address above.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PRINTF_UTIL_
#define PRINTF_UTIL_

#include <stdarg.h>
#include "time_util.h"

int printf_rate_limit(uint64_t limit, const char *format, ...)
{
    int ret = 0;
    static uint64_t last = 0;
    uint64_t now = utime_now();
    if(now > last + limit)
    {
        va_list args;
        va_start(args, format);
        last = now;
        ret = printf(format, args);
        va_end(args);
    }
    return ret;
}

#endif //PRINTF_UTIL_

